﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Petaframe.Generation;
using EnvDTE;

namespace Petaframe.GenerationExtension
{
    public partial class BSGeneratorForm : Form
    {
        public bool IsProjectCreated { get; set; }
        public string ProjectFileFullName { get; set; }

        public BSGeneratorForm()
        {
            InitializeComponent();
        }

        private void BSGeneratorForm_Load(object sender, EventArgs e)
        {
            txtBoxConnectionString.Text = @"DATA SOURCE=XE;PERSIST SECURITY INFO=False;USER ID=SOUTHWIND;PASSWORD=S123";

        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                txtBoxFilePath.Text = folderDialog.SelectedPath;            
        }

        private void btnLoadTables_Click(object sender, EventArgs e)
        {
            TablolariYukle();
        }

        private void TablolariYukle()
        {
            BSGenerator bsGenerator = new BSGenerator();
            bsGenerator.ConnectionString = txtBoxConnectionString.Text;
            bsGenerator.IsMSDataProvider = radioButtonMssql.Checked;
            IDataProvider dataProvider = bsGenerator.GetDataProvider();
            DataTable dt = dataProvider.TablolariYukle();
            
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("Veritabanında tablo bulunmamaktadır!");
                return;
            }

            listBoxTables.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                listBoxTables.Items.Add(row["TABLE_NAME"]);
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                GenerateBusinessClasses();                
            }
            catch (Exception exc)
            {
                MessageBox.Show("Hata Oluştu! Hata: " + exc.Message, "Hata", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void GenerateBusinessClasses()
        {
            if (String.IsNullOrEmpty(txtBoxConnectionString.Text))
            {
                MessageBox.Show("Veritabanı bağlantısını giriniz!", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (String.IsNullOrEmpty(txtBoxFilePath.Text))
            {
                MessageBox.Show("Dosya dizini giriniz!", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (String.IsNullOrEmpty(txtBoxNamespace.Text))
            {
                MessageBox.Show("Namespace giriniz!", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (listBoxTables.SelectedItems.Count == 0)
            {
                MessageBox.Show("Tablo seçiniz!", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            Generate();

            this.ProjectFileFullName = txtBoxFilePath.Text + "\\" + txtBoxNamespace.Text + "\\" + txtBoxNamespace.Text + ".csproj";
            this.Close();
        }

        private void Generate()
        {
            BSGenerator bsGenerator = new BSGenerator();
            bsGenerator.ConnectionString = txtBoxConnectionString.Text;
            bsGenerator.IsMSDataProvider = radioButtonMssql.Checked == true ? true : false;
            bsGenerator.Namespace = txtBoxNamespace.Text;
            bsGenerator.DataTypeNamespace = (!String.IsNullOrEmpty(txtDataTypeNamespace.Text)) ? txtDataTypeNamespace.Text : txtBoxNamespace.Text.Replace("Business", "DataType");
            bsGenerator.DataProvider = radioButtonMssql.Checked == true ? "DataProvider.SqlServer" : "DataProvider.Oracle";
            bsGenerator.FilePath = txtBoxFilePath.Text + "\\" + txtBoxNamespace.Text;
            bsGenerator.IsMSDataProvider = radioButtonMssql.Checked;

            List<string> classList = new List<string>();

            for (int i = 0; i < listBoxTables.SelectedItems.Count; i++)
            {
                classList.Add(listBoxTables.SelectedItems[i].ToString());
            }

            bsGenerator.classList = classList;
            bsGenerator.Generate();
        }
    }
}
